from web.Script import pic_netbian_com

from django.http import JsonResponse

from web import Success_Error_Dict

Script = pic_netbian_com.ScriptPic()


# 获取分类
def get_category(request):
    category = Script.get_category()
    return JsonResponse(category, safe = False, status = 200, content_type = 'application/json')

# 根据分类获取图片
def get_pic(request):
    response = Success_Error_Dict.Request_Error_Dict
    if request.method == 'GET':
        theme = request.GET.get('theme')
        if theme is None:
            response['msg'] = '请输入分类'
            return JsonResponse(response, status = 400, content_type = 'application/json')
        pic = Script.run(theme)
        return JsonResponse(pic, safe = False, status = 200, content_type = 'application/json')

    response['msg'] = '请求方式错误'
    return JsonResponse(response, status = 400, content_type = 'application/json')

# 搜索图片
def search(request):
    response = Success_Error_Dict.Request_Error_Dict
    if request.method == 'GET':
        keyword = request.GET.get('keyword')
        if keyword is None:
            response['msg'] = '请输入搜索关键字'
            return JsonResponse(response, status = 400, content_type = 'application/json')
        pic = Script.search(keyword)
        return JsonResponse(pic, safe = False, status = 200, content_type = 'application/json')
    response['msg'] = '请求方式错误'
    return JsonResponse(response, status = 400, content_type = 'application/json')

# 获取原图
def get_original_pic(request):
    response = Success_Error_Dict.Request_Error_Dict
    if request.method == 'GET':
        link = request.GET.get('link')
        if link is None:
            response['msg'] = '请输入图片链接'
            return JsonResponse(response, status = 400, content_type = 'application/json')
        pic = Script.get_original_pic(link)
        return JsonResponse(pic, safe = False, status = 200, content_type = 'application/json')
    response['msg'] = '请求方式错误'
    return JsonResponse(response, status = 400, content_type = 'application/json')
